﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experimentor
{
    public class ExperimentDef
    {
        public SortableBindingList<PersonAttribute> personAttributes;
        public DataTable slides;
        public SortableBindingList<Sequence> sequences;
        public SortableBindingList<UserdefFunction> userdefFunctions;
        
        public ExperimentDef()
        {
            this.personAttributes = new SortableBindingList<PersonAttribute>();
            this.slides = new DataTable("Slides");
            this.slides.Columns.Add("Order",typeof(Int32));
            this.slides.Columns.Add("Slide A",typeof(string));
            this.slides.TableNewRow += new DataTableNewRowEventHandler(this.slides_newRow);
            this.slides.RowDeleted += new DataRowChangeEventHandler(this.slides_deleteRow);
            this.sequences = new SortableBindingList<Sequence>();
            this.userdefFunctions = new SortableBindingList<UserdefFunction>();
        }

        public void SetHandler()
        {
            this.slides.TableNewRow += new DataTableNewRowEventHandler(this.slides_newRow);
            this.slides.RowDeleted += new DataRowChangeEventHandler(this.slides_deleteRow);
        }

        private void slides_newRow(object sender, DataTableNewRowEventArgs e)
        {
            this.slides.AcceptChanges();
            e.Row["Order"] = this.slides.Rows.Count + 1;
        }

        private void slides_deleteRow(object sender, DataRowChangeEventArgs e)
        {
            this.slides.AcceptChanges();
            for (int i = 0; i < this.slides.Rows.Count; i++) this.slides.Rows[i]["Order"] = i + 1;
        }

        public void slides_do_addColumn()
        {
            int numcolumns = this.slides.Columns.Count;
            char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            string columnname = "";
            while (numcolumns > 0)
            {
                columnname = alphabet[(numcolumns-1) % 26] + columnname;
                numcolumns = (numcolumns-1) / 26;
            }
            this.slides.Columns.Add("Slide " + columnname,typeof(string));
        }

        public bool hasSetNr (int nr)
        {
            return this.slides.Select("Order = " + nr.ToString()).Length > 0;
        }
    }
}
